home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Windows News 2010 Summer - Disc 1
/
WN_Ete2010_CD1.iso
/
Onglet5
/
Weezo
/
Weezo setup.exe
/
{code_appDir}
/
www
/
js
/
BigInt-RSA.js
< prev
next >
Wrap
Text File
|
2010-05-19
|
11KB
|
1 lines
var JSCallback;var encryptPercNode;var RSAResult;var timed_div,timed_idx,timed_result,timed_base=0;var blocks=new Array();var cyphStep=0,modulus,public_key,block;function RSACrypt(f,h,c){if(f.length==0){return h("")}JSCallback=h;RSAResult="";encryptPercNode=c;setMaxDigits(keyMaxDigits);modulus=biFromDecimal(keyModulo);public_key=biFromDecimal(keyPExp);var a;var e=0,g=0;var d=Math.ceil(keyLength/8);do{block=f.substr(e,d);e+=d;a=new Array();for(var b=0;b<block.length;b++){a.push(block.charCodeAt(b))}blocks[g]=pow_mod(binary_to_number(a),public_key,modulus);g++}while(e<f.length);timed_number_to_binary(keyLength/8,0,0)}function timed_number_to_binary(c,a,d){if(a>=blocks.length){return JSCallback("rsa:"+RSAResult)}if(d==0){timed_result=new Array();if(!timed_base){timed_base=biFromNumber(256)}timed_div=biCopy(blocks[a]);timed_idx=c-1}if(biCompare(timed_div,bigZero)>0){var b=biModulo(timed_div,timed_base);timed_div=biDivide(timed_div,timed_base);timed_result[timed_idx--]=parseInt(biToString(b,10));d++;if(encryptPercNode){encryptPercNode.innerHTML=Math.ceil(100*(d/c+a)/blocks.length)+"%"}if(d%3==0){setTimeout("timed_number_to_binary("+c+","+a+","+d+")",1)}else{timed_number_to_binary(c,a,d)}}else{RSAResult+=timed_result+";";timed_number_to_binary(c,a+1,0)}}function pow_mod(f,c,b){var e=new Array();var d=0;var a=biFromNumber(2);var g=biCopy(c);while(biCompare(g,bigZero)==1){rem=biModulo(g,a);g=biDivide(g,a);if(biCompare(rem,bigOne)==0){e.push(d)}d++}partial_results=new Array();part_res=biCopy(f);idx=0;for(k=0;k<e.length;k++){factor=e[k];while(idx<factor){part_res=biPow(part_res,2);part_res=biModulo(part_res,b);idx++}partial_results.push(part_res)}result=bigOne;for(k=0;k<partial_results.length;k++){part_res=partial_results[k];result=biMultiply(result,part_res);result=biModulo(result,b)}return result}function add_PKCS1_padding(c,b){var d=b-3-c.length;var a=new Array();a.push(0);a.push(2);for(i=0;i<d;i++){a.push(Math.round(Math.random()*255))}a.push(0);a=a.concat(c);return a}function binary_to_number(a){base=biFromNumber(256);result=biFromNumber(0);radix=biFromNumber(1);for(i=a.length-1;i>=0;i--){digit=biFromNumber(a[i]);part_res=biMultiply(digit,radix);result=biAdd(result,part_res);radix=biMultiply(radix,base)}return result}function number_to_binary(c,e){var d=biFromNumber(256);var b=new Array();var f=biCopy(c);var a=e-1;for(k=0;k<e;k++){b.push(0)}while(biCompare(f,bigZero)>0){mod=biModulo(f,d);f=biDivide(f,d);b[a--]=parseInt(biToString(mod,10))}return b}var biRadixBase=2;var biRadixBits=16;var bitsPerDigit=biRadixBits;var biRadix=1<<16;var biHalfRadix=biRadix>>>1;var biRadixSquared=biRadix*biRadix;var maxDigitVal=biRadix-1;var maxInteger=9999999999999998;var maxDigits;var ZERO_ARRAY;var bigZero,bigOne;function setMaxDigits(b){maxDigits=b;ZERO_ARRAY=new Array(maxDigits);for(var a=0;a<ZERO_ARRAY.length;a++){ZERO_ARRAY[a]=0}bigZero=new BigInt();bigOne=new BigInt();bigOne.digits[0]=1}setMaxDigits(20);var dpl10=15;var lr10=biFromNumber(1000000000000000);function BigInt(a){if(typeof a=="boolean"&&a==true){this.digits=null}else{this.digits=ZERO_ARRAY.slice(0)}this.isNeg=false}function biFromDecimal(e){var d=e.charAt(0)=="-";var c=d?1:0;var a;while(c<e.length&&e.charAt(c)=="0"){++c}if(c==e.length){a=new BigInt()}else{var b=e.length-c;var f=b%dpl10;if(f==0){f=dpl10}a=biFromNumber(Number(e.substr(c,f)));c+=f;while(c<e.length){a=biAdd(biMultiply(a,lr10),biFromNumber(Number(e.substr(c,dpl10))));c+=dpl10}a.isNeg=d}return a}function biCopy(b){var a=new BigInt(true);a.digits=b.digits.slice(0);a.isNeg=b.isNeg;return a}function biFromNumber(c){var a=new BigInt();a.isNeg=c<0;c=Math.abs(c);var b=0;while(c>0){a.digits[b++]=c&maxDigitVal;c=Math.floor(c/biRadix)}return a}function reverseStr(c){var a="";for(var b=c.length-1;b>-1;--b){a+=c.charAt(b)}return a}var hexatrigesimalToChar=new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");function biToString(d,f){var c=new BigInt();c.digits[0]=f;var e=biDivideModulo(d,c);var a=hexatrigesimalToChar[e[1].digits[0]];while(biCompare(e[0],bigZero)==1){e=biDivideModulo(e[0],c);digit=e[1].digits[0];a+=hexatrigesimalToChar[e[1].digits[0]]}return(d.isNeg?"-":"")+reverseStr(a)}function biToDecimal(d){var c=new BigInt();c.digits[0]=10;var e=biDivideModulo(d,c);var a=String(e[1].digits[0]);while(biCompare(e[0],bigZero)==1){e=biDivideModulo(e[0],c);a+=String(e[1].digits[0])}return(d.isNeg?"-":"")+reverseStr(a)}var hexToChar=new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");function digitToHex(c){var b=15;var a="";for(i=0;i<4;++i){a+=hexToChar[c&b];c>>>=4}return reverseStr(a)}function biToHex(b){var a="";var d=biHighIndex(b);for(var c=biHighIndex(b);c>-1;--c){a+=digitToHex(b.digits[c])}return a}function charToHex(l){var d=48;var b=d+9;var e=97;var h=e+25;var g=65;var f=65+25;var a;if(l>=d&&l<=b){a=l-d}else{if(l>=g&&l<=f){a=10+l-g}else{if(l>=e&&l<=h){a=10+l-e}else{a=0}}}return a}function hexToDigit(d){var b=0;var a=Math.min(d.length,4);for(var c=0;c<a;++c){b<<=4;b|=charToHex(d.charCodeAt(c))}return b}function biFromHex(e){var b=new BigInt();var a=e.length;for(var d=a,c=0;d>0;d-=4,++c){b.digits[c]=hexToDigit(e.substr(Math.max(d-4,0),Math.min(d,4)))}return b}function biFromString(m,l){var a=m.charAt(0)=="-";var e=a?1:0;var n=new BigInt();var b=new BigInt();b.digits[0]=1;for(var d=m.length-1;d>=e;d--){var f=m.charCodeAt(d);var g=charToHex(f);var h=biMultiplyDigit(b,g);n=biAdd(n,h);b=biMultiplyDigit(b,l)}n.isNeg=a;return n}function biDump(a){return(a.isNeg?"-":"")+a.digits.join(" ")}function biAdd(b,g){var a;if(b.isNeg!=g.isNeg){g.isNeg=!g.isNeg;a=biSubtract(b,g);g.isNeg=!g.isNeg}else{a=new BigInt();var f=0;var e;for(var d=0;d<b.digits.length;++d){e=b.digits[d]+g.digits[d]+f;a.digits[d]=e%biRadix;f=Number(e>=biRadix)}a.isNeg=b.isNeg}return a}function biSubtract(b,g){var a;if(b.isNeg!=g.isNeg){g.isNeg=!g.isNeg;a=biAdd(b,g);g.isNeg=!g.isNeg}else{a=new BigInt();var f,e;e=0;for(var d=0;d<b.digits.length;++d){f=b.digits[d]-g.digits[d]+e;a.digits[d]=f%biRadix;if(a.digits[d]<0){a.digits[d]+=biRadix}e=0-Number(f<0)}if(e==-1){e=0;for(var d=0;d<b.digits.length;++d){f=0-a.digits[d]+e;a.digits[d]=f%biRadix;if(a.digits[d]<0){a.digits[d]+=biRadix}e=0-Number(f<0)}a.isNeg=!b.isNeg}else{a.isNeg=b.isNeg}}return a}function biHighIndex(b){var a=b.digits.length-1;while(a>0&&b.digits[a]==0){--a}return a}function biNumBits(c){var f=biHighIndex(c);var e=c.digits[f];var b=(f+1)*bitsPerDigit;var a;for(a=b;a>b-bitsPerDigit;--a){if((e&32768)!=0){break}e<<=1}return a}var inside_pow_mod=0;function biMultiply(h,g){var o=new BigInt();var f;var b=biHighIndex(h);var m=biHighIndex(g);var l,a,d;for(var e=0;e<=m;++e){f=0;d=e;for(j=0;j<=b;++j,++d){a=o.digits[d]+h.digits[j]*g.digits[e]+f;o.digits[d]=a&maxDigitVal;f=a>>>biRadixBits}o.digits[e+b+1]=f}o.isNeg=h.isNeg!=g.isNeg;return o}function biMultiplyDigit(a,g){var f,e,d;result=new BigInt();f=biHighIndex(a);e=0;for(var b=0;b<=f;++b){d=result.digits[b]+a.digits[b]*g+e;result.digits[b]=d&maxDigitVal;e=d>>>biRadixBits}result.digits[1+f]=e;return result}function arrayCopy(e,h,c,g,f){var a=Math.min(h+f,e.length);for(var d=h,b=g;d<a;++d,++b){c[b]=e[d]}}var highBitMasks=new Array(0,32768,49152,57344,61440,63488,64512,65024,65280,65408,65472,65504,65520,65528,65532,65534,65535);function biShiftLeft(b,h){var d=Math.floor(h/bitsPerDigit);var a=new BigInt();arrayCopy(b.digits,0,a.digits,d,a.digits.length-d);var g=h%bitsPerDigit;var c=bitsPerDigit-g;for(var e=a.digits.length-1,f=e-1;e>0;--e,--f){a.digits[e]=((a.digits[e]<<g)&maxDigitVal)|((a.digits[f]&highBitMasks[g])>>>(c))}a.digits[0]=((a.digits[e]<<g)&maxDigitVal);a.isNeg=b.isNeg;return a}var lowBitMasks=new Array(0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535);function biShiftRight(b,h){var c=Math.floor(h/bitsPerDigit);var a=new BigInt();arrayCopy(b.digits,c,a.digits,0,b.digits.length-c);var f=h%bitsPerDigit;var g=bitsPerDigit-f;for(var d=0,e=d+1;d<a.digits.length-1;++d,++e){a.digits[d]=(a.digits[d]>>>f)|((a.digits[e]&lowBitMasks[f])<<g)}a.digits[a.digits.length-1]>>>=f;a.isNeg=b.isNeg;return a}function biMultiplyByRadixPower(b,c){var a=new BigInt();arrayCopy(b.digits,0,a.digits,c,a.digits.length-c);return a}function biDivideByRadixPower(b,c){var a=new BigInt();arrayCopy(b.digits,c,a.digits,0,a.digits.length-c);return a}function biModuloByRadixPower(b,c){var a=new BigInt();arrayCopy(b.digits,0,a.digits,0,c);return a}function biCompare(a,c){if(a.isNeg!=c.isNeg){return 1-2*Number(a.isNeg)}for(var b=a.digits.length-1;b>=0;--b){if(a.digits[b]!=c.digits[b]){if(a.isNeg){return 1-2*Number(a.digits[b]>c.digits[b])}else{return 1-2*Number(a.digits[b]<c.digits[b])}}}return 0}function biDivideModulo(g,f){var a=biNumBits(g);var e=biNumBits(f);var d=f.isNeg;var p,o;if(a<e){if(g.isNeg){p=biCopy(bigOne);p.isNeg=!f.isNeg;g.isNeg=false;f.isNeg=false;o=biSubtract(f,g);g.isNeg=true;f.isNeg=d}else{p=new BigInt();o=biCopy(g)}return new Array(p,o)}p=new BigInt();o=g;var l=Math.ceil(e/bitsPerDigit)-1;var h=0;while(f.digits[l]<biHalfRadix){f=biShiftLeft(f,1);++h;++e;l=Math.ceil(e/bitsPerDigit)-1}o=biShiftLeft(o,h);a+=h;var v=Math.ceil(a/bitsPerDigit)-1;var C=biMultiplyByRadixPower(f,v-l);while(biCompare(o,C)!=-1){++p.digits[v-l];o=biSubtract(o,C)}for(var A=v;A>l;--A){var m=(A>=o.digits.length)?0:o.digits[A];var B=(A-1>=o.digits.length)?0:o.digits[A-1];var z=(A-2>=o.digits.length)?0:o.digits[A-2];var w=(l>=f.digits.length)?0:f.digits[l];var c=(l-1>=f.digits.length)?0:f.digits[l-1];if(m==w){p.digits[A-l-1]=maxDigitVal}else{p.digits[A-l-1]=Math.floor((m*biRadix+B)/w)}var u=p.digits[A-l-1]*((w*biRadix)+c);var s=(m*biRadixSquared)+((B*biRadix)+z);while(u>s){--p.digits[A-l-1];u=p.digits[A-l-1]*((w*biRadix)|c);s=(m*biRadix*biRadix)+((B*biRadix)+z)}C=biMultiplyByRadixPower(f,A-l-1);o=biSubtract(o,biMultiplyDigit(C,p.digits[A-l-1]));if(o.isNeg){o=biAdd(o,C);--p.digits[A-l-1]}}o=biShiftRight(o,h);p.isNeg=g.isNeg!=d;if(g.isNeg){if(d){p=biAdd(p,bigOne)}else{p=biSubtract(p,bigOne)}f=biShiftRight(f,h);o=biSubtract(f,o)}if(o.digits[0]==0&&biHighIndex(o)==0){o.isNeg=false}return new Array(p,o)}function biDivide(a,b){return biDivideModulo(a,b)[0]}function biModulo(a,b){return biDivideModulo(a,b)[1]}function biMultiplyMod(b,c,a){return biModulo(biMultiply(b,c),a)}function biPow(c,e){var b=bigOne;var d=c;while(true){if((e&1)!=0){b=biMultiply(b,d)}e>>=1;if(e==0){break}d=biMultiply(d,d)}return b}function biPowMod(d,g,c){var b=bigOne;var e=d;var f=g;while(true){if((f.digits[0]&1)!=0){b=biMultiplyMod(b,e,c)}f=biShiftRight(f,1);if(f.digits[0]==0&&biHighIndex(f)==0){break}e=biMultiplyMod(e,e,c)}return b};